﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseData;
using BaseData.UI;
using TaxBiz.SysConfig;
using TaxBiz.Tax;
using TaxEntity.Tax;
using TaxEntity.SysConfig;
using TaxBiz.User;
using TaxEntity.User;

namespace Admin.Zonghe
{
    public partial class Query : BaseCommon.BasePage
    {
        protected int pageSize = 10;
        protected int page = 1;
        protected int allCount = 0;
        protected string pageUrl = string.Empty;
        protected string strWhere = string.Empty;
        protected string queryConditionDesc = string.Empty;
        protected string certificateID = string.Empty;//名称
        protected string name = string.Empty;
        protected string idCardNum = string.Empty;
        protected string departmentID = string.Empty;
        protected string managerID = string.Empty;
        protected string diduanID = string.Empty;
        protected string xiangzhenID = string.Empty;
        protected string jiedaoID = string.Empty;
        protected string houseType = string.Empty; //类型
        protected string isTaxOK = string.Empty;
        protected string rentName = string.Empty;
        protected string rentIDCardNum = string.Empty;
        protected string startDate = string.Empty;
        protected string endDate = string.Empty;
        protected string year = string.Empty;
        protected string outputFields = string.Empty;
        protected string outputContent = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HasPower())
            {
                Jscript.AlertAndGoBack("您权限不足，不能操作");
                return;
            }
            this.InitData();
            string action = PageContext.GetRequest("action");
            if (action.ToLower() == "download")
            {
                this.DownLoad();
            }
            else
            {
                outputContent = this.BindData();
            }
        }

        protected void InitData()
        {
            pageUrl = Request.Url.AbsolutePath;
            //当前页码
            if (PageContext.GetRequest("page") != string.Empty)
            {
                int.TryParse(PageContext.GetRequest("page"), out page);
            }
            pageUrl += "?page=" + page;
            pageUrl += "&action=query";
            //名称
            certificateID = PageContext.GetRequest("txtCertificateID");
            if (certificateID != string.Empty)
            {
                pageUrl += "&txtCertificateID=" + certificateID;
                strWhere += " and certificateID = '" + certificateID + "'";
                queryConditionDesc += "房产证号：" + certificateID + "，";
            }
            name = PageContext.GetRequest("txtName");
            if (name != string.Empty)
            {
                pageUrl += "&txtName=" + name;
                strWhere += " and name = '" + name + "'";
                queryConditionDesc += "房主姓名：" + name + "，";
            }
            idCardNum = PageContext.GetRequest("txtIDCardNum");
            if (idCardNum != string.Empty)
            {
                pageUrl += "&txtIDCardNum=" + idCardNum;
                strWhere += " and idCardNum = '" + idCardNum + "'";
                queryConditionDesc += "房主身份证：" + idCardNum + "，";
            }
            int tempInt = 0;
            departmentID = PageContext.GetRequest("selDepart");
            if (departmentID != string.Empty && int.TryParse(departmentID, out tempInt))
            {
                pageUrl += "&selDepart=" + departmentID;
                strWhere += " and departmentID = " + departmentID + "";
                DepartmentEntity dme = new DepartmentBiz().GetEntityById(tempInt);
                if (dme != null)
                {
                    queryConditionDesc += "部门：" + dme.Name + "，";
                }
            }
            //只能查询本部门数据权限
            if (isQueryDepartmentOnly)
            {
                strWhere += " and departmentID = " + LoginDepartmentID + "";
            }
            long tempLong = 0;
            managerID = PageContext.GetRequest("selManager");
            if (managerID != string.Empty && long.TryParse(managerID, out tempLong))
            {
                pageUrl += "&selManager=" + managerID;
                strWhere += " and managerID = " + managerID + "";
                UserEntity ue = new UserBiz().GetEntityById(tempLong);
                if (ue != null)
                {
                    queryConditionDesc += "管理员：" + ue.SName + "，";
                }
            }
            xiangzhenID = PageContext.GetRequest("selXiangzhen");
            if (xiangzhenID != string.Empty && int.TryParse(xiangzhenID, out tempInt))
            {
                pageUrl += "&selXiangzhen=" + xiangzhenID;
                strWhere += " and xiangzhenID = " + xiangzhenID + "";
                XiangzhenEntity xze = new XiangzhenBiz().GetEntityById(tempInt);
                if (xze != null)
                {
                    queryConditionDesc += "乡镇：" + xze.Name + "，";
                }
            }
            jiedaoID = PageContext.GetRequest("selJiedao");
            if (jiedaoID != string.Empty && int.TryParse(jiedaoID, out tempInt))
            {
                pageUrl += "&selJiedao=" + jiedaoID;
                strWhere += " and jiedaoID = " + jiedaoID + "";
                JiedaoEntity jde = new JiedaoBiz().GetEntityById(tempInt);
                if (jde != null)
                {
                    queryConditionDesc += "街道：" + jde.Name + "，";
                }
            }
            diduanID = PageContext.GetRequest("selDiduan");
            if (diduanID != string.Empty && int.TryParse(diduanID, out tempInt))
            {
                pageUrl += "&selDiduan=" + diduanID;
                strWhere += " and diduanID = " + diduanID + "";
                DiDuanEntity de = new DiDuanBiz().GetEntityById(tempInt);
                if (de != null)
                {
                    queryConditionDesc += "地段：" + de.Name + "，";
                }
            }
            houseType = PageContext.GetRequest("selHouseType");
            if (houseType != string.Empty)
            {
                pageUrl += "&selHouseType=" + houseType;
                strWhere += " and houseType = '" + houseType + "'";
                queryConditionDesc += "房屋类型：" + houseType + "，";
            }
            isTaxOK = PageContext.GetRequest("selIsTaxOK");
            if (isTaxOK != string.Empty)
            {
                pageUrl += "&selIsTaxOK=" + isTaxOK;
                strWhere += " and TaxStatus = " + isTaxOK + "";
                queryConditionDesc += "完税状态：" + isTaxOK == "1" ? "完税" : "未完税" + "，";
            }
            rentName = PageContext.GetRequest("txtRentName");
            if (rentName != string.Empty)
            {
                pageUrl += "&txtRentName=" + rentName;
                strWhere += " and RentName = '" + rentName + "'";
                queryConditionDesc += "承租人姓名：" + rentName + "，";
            }
            rentIDCardNum = PageContext.GetRequest("txtRentIDCardNum");
            if (rentIDCardNum != string.Empty)
            {
                pageUrl += "&txtRentIDCardNum=" + rentIDCardNum;
                strWhere += " and RentIDCardNum = '" + rentIDCardNum + "'";
                queryConditionDesc += "承租人身份证：" + rentIDCardNum + "，";
            }
            DateTime tempDate = DateTime.Now;
            startDate = PageContext.GetRequest("txtStartDate");
            if (!string.IsNullOrEmpty(startDate) && DateTime.TryParse(startDate, out tempDate))
            {
                pageUrl += "&txtStartDate=" + startDate;
                strWhere += " and startDate = '" + startDate + "'";
                queryConditionDesc += "承租开始日期：" + startDate + "，";
            }
            endDate = PageContext.GetRequest("txtEndDate");
            if (!string.IsNullOrEmpty(endDate) && DateTime.TryParse(endDate, out tempDate))
            {
                pageUrl += "&txtEndDate=" + endDate;
                strWhere += " and endDate = '" + endDate + "'";
                queryConditionDesc += "承租结束日期：" + endDate + "，";
            }
            year = PageContext.GetRequest("selYear");
            if (year != string.Empty)
            {
                pageUrl += "&selYear=" + year;
                strWhere += " and year = " + year + "";
                queryConditionDesc += "税款属期：" + year + "年，";
            }
            queryConditionDesc = queryConditionDesc.TrimEnd(new char[] { ',' , '，'});
            if (queryConditionDesc == string.Empty)
            {
                queryConditionDesc = "全部数据";
            }
            pageUrl += "&ckOutputFields=" + PageContext.GetRequest("ckOutputFields");
        }

        protected string BindData()
        {
            StringBuilder sb = new StringBuilder();
            outputFields = PageContext.GetRequest("ckOutputFields");
            if (string.IsNullOrEmpty(outputFields))
            {
                return string.Empty;
            }
            DataTable dt = ZongheQueryBiz.GetList(strWhere, pageSize, page, ref allCount);
            if (dt == null)
            {
                return string.Empty;
            }
            //设置分页参数
            this.pageCountrol.CurPage = page;
            this.pageCountrol.PageSize = pageSize;
            this.pageCountrol.AllCount = allCount;
            this.pageCountrol.PageUrl = pageUrl;
            this.pageCountrol.PageName = "page";
            string[] arrOutputFileds = outputFields.Split(',');
            //标题
            ZongheQueryEntity zqe = new ZongheQueryEntity();
            Dictionary<string, string> dicQuery = zqe.QueryDic;
            sb.Append("<tr>");
            foreach (string s in arrOutputFileds)
            {
                string filedName = dicQuery.ContainsKey(s) ? dicQuery[s] : s;
                sb.AppendFormat("<th height='30' align='center' valign='middle' bgcolor='#DAF0FE'>{0}</th>", filedName);
            }
            sb.Append("<th height='30' align='center' valign='middle' bgcolor='#DAF0FE'>详情</th>");
            sb.Append("</tr>");
            sb.Append("</tr>");
            //内容
            foreach (DataRow dr in dt.Rows)
            {
                sb.Append("<tr>");
                for (int i = 0; i < arrOutputFileds.Length; i++)
                {
                    sb.AppendFormat("<td align='center'>{0}</td>", dr[arrOutputFileds[i]]);
                }
                sb.AppendFormat("<td align='center'><a href='QueryDetail.aspx?width=720&height=400&id={0}&rentid={1}' class='thickbox'><img src='/Admin/Images/bluestyle/view.gif' alt='点击查看出租屋详情' width='15' height='15' border='0' /></a></td>", dr["Id"], dr["rentid"]);
                sb.Append("</tr>");
            }

            return sb.ToString();
        }

        //下载报表
        protected void DownLoad()
        {
            string fileName = "房产信息" + DateTime.Now.ToString("yyyyMMdd");
            outputFields = PageContext.GetRequest("ckOutputFields");
            if (string.IsNullOrEmpty(outputFields))
            {
                return;
            }
            DataTable dt = ZongheQueryBiz.GetList(strWhere, pageSize, page, ref allCount);
            if (dt == null)
            {
                return;
            }
            string[] fields = outputFields.Split(',');
            string[] titles = new string[fields.Length];
            ZongheQueryEntity zqe = new ZongheQueryEntity();
            Dictionary<string, string> dicQuery = zqe.QueryDic;
            int k = 0;
            foreach (string s in fields)
            {
                string title = dicQuery.ContainsKey(s) ? dicQuery[s] : s;
                titles[k] = title;
                k++;
            }
            //处理身份证导出Excel科学计数法问题
            for (int i = 0; dt != null && i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["IDCardNum"] = "'" + dt.Rows[i]["IDCardNum"];
                dt.Rows[i]["RentIDCardNum"] = "'" + dt.Rows[i]["RentIDCardNum"];
            }
            ToExcel(dt, fileName, titles, fields);
        }
    }
}